<?php
    session_start();
    $nperpage = 3;

    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }
/*
    function getUserNamePage(){
        $url = curPageURL();
        $j = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '?'){
                $j = $i + 1;
            }
        }

        if ($j != -1){
            return substr($url, $j);
        }
        else{
            return "";
        }

    }
*/
    function getUserNamePage2(){
        $url = curPageURL();
        $j = -1;
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '?'){
                $j = $i + 1;
            }
            if ($url[$i] == '&'){
                $k = $i;
            }
        }

        if (($j != -1) && ($k > $j)){
            return substr($url, $j, $k - $j);
        }
        else if (($j != -1) && ($k == -1)){
            return substr($url, $j);
        }
        else{
            return "";
        }

    }

    function getIdThread(){
        $id = getIdThread2();
        if ($id != NULL)
	{
		$ada = true;
	}
	else{
		$ada = false;
	}
	$idthread = $_GET["id"];
	if ($idthread==NULL&&$ada){
		$idthread=$id;
	}
        return $idthread;
    }

    function getIdThread2(){
        $url = curPageURL();
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '&'){
                $k = $i + 1;
            }
        }
        if ($k != -1){
            return substr($url, $k);
        }
        else{
            return "";
        }

    }

    function sendpesan(){
        require_once 'php/database.php';
        $idthreadsaatini = getIdThread();
        $user = $_SESSION['username'];

        database::query("INSERT TO message (Pengirim, Pesan, IDThread, TanggalKirim)
            VALUES ('".$user."', '".$_POST[pesanarea]."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");
        /*mysql_query("INSERT TO message (Pengirim, Pesan, IDThread, TanggalKirim)
            VALUES ('".$user."', '".$_POST[pesanarea]."', '".$idthreadsaatini."', CURRENT_TIMESTAMP )");*/
    }
?>
    
<?php
        echo '<div id="messagearea">';
            echo '<div class="messagedetailcontent">';
                echo '<div id="messagedetailcontentajax1">';
                
                    require_once 'php/database.php';
                    require_once 'php/photo.php';
                    $idthread = getIdThread();
                    $user = getUserNamePage();
                    
                    $hasil1 = database::query("SELECT * FROM user WHERE Username = '$user'");
                    if(mysql_num_rows($hasil1)>0){
                        $baris1 = photo::getProfPic($user);
                        $row = mysql_fetch_array($baris1);
                        if ($row["FileFoto"] == NULL){
                            $gambar = 'images/bg-photo.png';
                        }
                        else{
                            $gambar = 'users/'.$loguname.'/'.$row["FileFoto"];
                        }

                        /*$baris1 = mysql_fetch_row($hasil1);
                        if ($baris1[8] == NULL){
                            $gambar = 'images/bg-photo.png';
                        }
                        else{
                            $idgambar = $baris[8];
                            $hasil2 = mysql_query("SELECT * FROM Foto WHERE IDFOTO = $idgambar", $con);
                            if(mysql_num_rows($hasi2)>0){
                                $baris2 = mysql_fetch_row($hasil2);
                                $gambar= 'users/'.$baris2[1].'/'.$baris2[2];
                            }
                        }*/
                    }

                    echo '<div class="box">';
                        echo '<table align="center">';
                            echo '<tr>';
                                echo '<form action="messagepost.php?un='.$user.'&id='.$idthread.'" method="post" name="kirimpesan">';
                                    echo '<td>';
                                        echo '<img id="foto" src="'.$gambar.'" border="0" height="75" width="75"/>';
                                    echo '</td>';
                                    echo '<td id="pesansiap">';
                                        echo '<label style="font-size: 10px; float: left;">';
                                            echo 'Insert your message below.';
                                        echo '</label>';
                                        echo '<div>';
                                            echo '<textarea class="textpesan" name="pesanarea" id="pesanarea"></textarea>';
                                        echo '</div>';
                                        echo '<div>';
                                            //echo '<input type="submit" style="float: right;" id="kirimpesan" value="Send"/>';
                                            echo '<br/><div class="button180" style="float: right;" onclick="sendpesan()">Send</div>';
                                        echo '</div>';
                                    echo '</td>';
                                echo '</form>';
                            echo '</tr>';
                        echo '</table>';
                    echo '</div>';
                    echo '</div>';
                    
                    echo '<div id="messagedetailcontentajax2">';
                    
                        $page = 0;
                    
                        echo '<br/><br/>';
                        echo '<div align="center">';
                            echo '<input type="button" id="previsipesan" name="previsipesan" value="prev" onclick="previsipes('.$page.',\''.$user.'\','.$idthread.')">';

                            echo '</input>';
                        echo '</div>';
                        

                    $hasil3 = database::query("SELECT * FROM message WHERE IDThread = '$idthread' ORDER BY TanggalKirim DESC");
                    if(mysql_num_rows($hasil3)>0){
                        $jum = 0;
                        while ($baris3 = mysql_fetch_row($hasil3)){
                            $pengirim = $baris3[0];
                            $tanggalkirim = $baris3[3];
                            $hasil4 = database::query("SELECT * FROM user WHERE Username = '$pengirim'");
                            if(mysql_num_rows($hasil4)>0){
                                $baris4 = mysql_fetch_row($hasil4);
                                $namapengirim = $baris4[3];

                                if($baris4[8] == NULL){
                                    $gambarwajah = 'images/bg-photo.png';
                                }
                                else{
                                    $idgambarwajah = $baris4[8];
                                    $hasil5 = database::query("SELECT * FROM Foto WHERE IDFOTO = '$idgambarwajah'");
                                    if(mysql_num_rows($hasil5)>0){
                                        $baris5 = mysql_fetch_row($hasil5);
                                        $gambarwajah= 'users/'.$baris5[1].'/'.$baris5[2];
                                    }
                                }

                                $namapengirimArray[$jum] = $namapengirim;
                                $gambarwajahArray[$jum] = $gambarwajah;
                                $tanggalkirimArray[$jum] = $tanggalkirim;
                                $isipesanArray[$jum] = $baris3[1];
                                $pengirimArray[$jum] = $pengirim;

                                ++$jum;
                            }
                            //++$jum;
                        }

                        if (($jum % $nperpage) ==  0){
                            $ntemp = (($jum - ($jum % $nperpage)) / $nperpage);
                        }
                        else{
                            $ntemp = (($jum - ($jum % $nperpage)) / $nperpage) + 1;
                        }
                        if ($ntemp > 0){
                            $npage = $ntemp - 1;
                        }

                        for ($j = 0; $j < $nperpage; ++$j){
                            $j2 = $page * $nperpage + $j;
                            if ($j2 < $jum){
                                //echo '<br/>';
                                echo '<div class="box2">';
                                    echo '<table align="center">';
                                        echo '<tr>';
                                            echo '<td>';
                                                echo '<img id="gambarpengirim_'.$j2.'" src="'.$gambarwajahArray[$j2].'" border="0" height="75" width="75"/>';
                                            echo '</td>';
                                            echo '<td>';
                                                echo '<div align="center" id="pesanmasuk">';
                                                    echo '<div id="tanggal">';
                                                        echo '<label>';
                                                            echo 'Tanggal Kirim: '.$tanggalkirimArray[$j2];
                                                        echo '</label>';
                                                    echo '</div>';
                                                    echo '<div>';
                                                        echo '<textarea id="pesan" disabled="disable" name="pesan">';
                                                            echo ''.$isipesanArray[$j2];
                                                        echo '</textarea>';
                                                    echo '</div>';
                                                    echo '<div id="pengirim">';
                                                        echo '<a href="profile.php?'.$pengirimArray[$j2].'"><label> '.$namapengirimArray[$j2].' </label></a>';
                                                    echo '</div>';
                                                echo '</div>';
                                            echo '</td>';
                                        echo '</tr>';
                                    echo '</table>';
                                echo '</div>';
                            }
                        }

                            echo '<div align="center">';
                                echo '<input type="button" id="nextisipesan" name="nextisipesan" value="next" onclick="nextisipes('.$page.',\''.$user.'\','.$npage.','.$idthread.')">';

                                echo '</input>';
                            echo '</div>';

                        echo '</div>';


                        echo '<div class="button180" onclick="backtomessages(\''.$user.'\')" style="margin-left: 20px;">Back to messages.</div>';
                    }
                    
                



            echo '</div>';
        echo '</div>';
    echo '</div>';
?>
    


